package com.lqian.tcp;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServe {

    public static void main(String[] args) {
        try{
        //1.建立socket连接 指定端口号
        ServerSocket serverSocket = new ServerSocket(9999);
        int count = 0;// 记录客户端的数量
        System.out.println("服务器启动，等待客户端的连接。。。");
        Socket socket = null;

        while(true){
            //接受连接
            socket=serverSocket.accept();
            //统计在线数
            ++count;
            //线程处理类 ，服务器对socket进行处理的线程
            Thread serverHandleThread=new Thread(new ServerHandleThread(socket));
            // 与main 异步的关系
            serverHandleThread.setPriority(4);
            serverHandleThread.start();
            System.out.println("上线的客户端有" + count + "个！");
            InetAddress inetAddress = socket.getInetAddress();
            System.out.println("当前客户端的IP地址是："+inetAddress.getHostAddress());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}
